package com.jd.mti.beetl.test.load;

import com.jd.mti.beetl.test.io.CharStream;
import freemarker.cache.ClassTemplateLoader;
import freemarker.template.TemplateException;

import java.io.IOException;
import java.util.Map;

/**
 * @Author sizegang
 * @Date 2021/11/14 20:29
 * @Version 1.0
 */
public class RenderFreemark extends LoadPare {
    static freemarker.template.Configuration configuration ;

    public static void loadFreemakrConfig(){
        configuration = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_22);
        configuration.setTemplateLoader(new ClassTemplateLoader(RenderBeetl.class, "/"));
    }
    public static void loadFreemark() throws IOException, TemplateException {
        loadFreemakrConfig();
        Map map = testData();

        long startTime = System.currentTimeMillis();

        for (int i = 0; i < 200; i++) {
            CharStream writer = new CharStream("UTF-8");
            freemarker.template.Template template = configuration.getTemplate("templates/freemarker.ftl");
            template.process(map, writer);
        }

        long cost = System.currentTimeMillis() - startTime;
        System.out.println("10000次渲染, freemarker 消耗时间" +cost + "毫秒");
    }

    public static void main(String[] args) {
        try {
            loadFreemark();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
    }
}
